<?php

namespace app\api\controller;

use app\admin\model\Order;
use app\admin\model\Subscribe;
use app\common\controller\Api;
use app\common\model\CashLog;
use think\Db;

/**
 * 交易记录接口
 */
class Record extends Api
{

    protected $noNeedLogin = ['index','policy','get_chip'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     * 
     */
    public function index()
    {
        $this->success('请求成功');
    }

    /*
     * 金额明细/交易记录
     * */
    public function cash_record_list()
    {
        $user = $this->auth->getUser();
        $type = $this->request->request('type');
        $page = $this->request->request('page');
        $list_rows = $this->request->request('list_rows');

        if (!$type) $this->error('类型type不能为空');

        $page = $page ? $page : 1;
        $list_rows = $list_rows ? $list_rows : config('site.list_rows');

        if ($type == config('CashType.ExchangeIn'))
        {
            $type = [config('CashType.ExchangeIn'),config('CashType.ExchangeOut')];
        }
        $type = $type==0 ? config('CashType') : $type;

        $where['user_id'] = ['=' , $user->id];
        $where['type'] = ['in' , $type];

        $order = 'time desc,value desc';

        $cash_log = new CashLog();

        $cash_list = $cash_log
            ->where($where)
            ->order($order)
            ->page($page,$list_rows)
            ->select();

        $this->success('金额明细/交易记录',$cash_list);
    }

    /*
     * 云币明细
     * */
    public function coin_record_list()
    {
        $user = $this->auth->getUser();
        $type = $this->request->request('type');
        $page = $this->request->request('page');
        $list_rows = $this->request->request('list_rows');

        $type = $type ? $type : config('OrderType');
        $page = $page ? $page : 1;
        $list_rows = $list_rows ? $list_rows : config('site.list_rows');

        $where['user_id'] = ['=' , $user->id];
        $where['type'] = ['in' , $type];

        $field = 'o.order_id,o.owner_id,o.user_id,o.cash,o.coin,o.detail,o.type,o.status,o.time,u.invite_code';

        if ($type == config('OrderType.Other'))
        {
            //  云币其他类型
            $field = 'o.order_id,o.user_id,o.coin,o.detail,o.status,o.time,u.invite_code';
        }

        $order = ['time'=>'desc','cash'=>'desc','coin'=>'desc'];

        //  查询列表
        $order_list = Db::name('order')
            ->alias('o')
            ->field($field)
            ->join('user u','o.user_id = u.id','left')
            ->where($where)
            ->order($order)
            ->page($page,$list_rows)
            ->select();

        $statusArr = ['0'  =>  '无效', '1'  =>  '未到账', '2'  =>  '已到账', '3'  =>  '其他'];
        foreach ($order_list as $k=>$v)
        {
            $order_list[$k]['time'] = date('Y-m-d H:i:s',$v['time']);
            $order_list[$k]['status'] = $statusArr[$v['status']] ? $statusArr[$v['status']] : '无效';
            $order_list[$k]['user_id'] = $v['invite_code'];
        }

        $this->success('云币明细列表',$order_list);
    }
}
